<?php
declare (strict_types = 1);

namespace app\api\controller;
use app\BaseController;


class GeTui extends BaseController
{


    private $AppID;
    private $AppSecret;
    private $AppKey;
    private $MasterSecret;
    private $HOST;

    public function __construct()
    {
        $this->AppID = config('params.getui.AppID');
        $this->AppSecret = config('params.getui.AppSecret');
        $this->AppKey = config('params.getui.AppKey');
        $this->MasterSecret = config('params.getui.MasterSecret');
        $this->HOST = config('params.getui.HOST');
    }

    function pushMessageToSingle($cid,$Message,$type){
        $igt = new \IGeTui($this->HOST,$this->AppKey,$this->MasterSecret);

        //消息模版：
        // 4.NotyPopLoadTemplate：通知弹框下载功能模板
        if($type == 'ios'){
            $template = $this->IGtTransmissionTemplateDemo($Message);
        }else{
            $template = $this->IGtNotificationTemplateDemo($Message);
        }

        //定义"SingleMessage"
        $message = new \IGtSingleMessage();
        $message->set_isOffline(true);//是否离线
        $message->set_offlineExpireTime(10 * 60 * 1000);//离线时间
        $message->set_data($template);//设置推送消息类型
        $message->set_PushNetWorkType(0);//设置是否根据WIFI推送消息，2为4G/3G/2G，1为wifi推送，0为不限制推送
        //接收方
        $target = new \IGtTarget();
        $target->set_appId($this->AppID);
        $target->set_clientId($cid);
        try {
            $rep = $igt->pushMessageToSingle($message, $target);
            return $rep;
        }catch(RequestException $e){
            $requstId =e.getRequ();
            //失败时重发
            $rep = $igt->pushMessageToSingle($message, $target,$requstId);
            return $rep;
        }
    }

    function IGtTransmissionTemplateDemo($Message){
        $template =  new \IGtTransmissionTemplate();
        $template->set_appId($this->AppID);//应用appid
        $template->set_appkey($this->AppKey);//应用appkey
        $template->set_transmissionType(1);//透传消息类型
        $template->set_transmissionContent("預約提示：".$Message);//透传内容
//          APN高级推送
        $apn = new \IGtAPNPayload();
        $alertmsg=new \DictionaryAlertMsg();
        $alertmsg->body = $Message;
        $alertmsg->actionLocKey="ActionLockey";
        $alertmsg->locKey="LocKey";
        $alertmsg->locArgs=array("locargs");
        $alertmsg->launchImage="launchimage";
        //  IOS8.2 支持
//        $alertmsg->title="DaDolce：";
//        $alertmsg->titleLocKey="TitleLocKey";
        $alertmsg->titleLocArgs=array("TitleLocArg");
        $alertmsg->subtitle="預約提示：";
        $alertmsg->subtitleLocKey="subtitleLocKey";
        $alertmsg->subtitleLocArgs=array("subtitleLocArgs");

        $apn->alertMsg=$alertmsg;
        $apn->badge=7;
        $apn->sound="";
        $apn->add_customMsg("payload","payload");
        //设置语音播报类型，int类型，0.不可用 1.播放body 2.播放自定义文本
        $apn->voicePlayType = 2;
        //设置语音播报内容，String类型，非必须参数，用户自定义播放内容，仅在voicePlayMessage=2时生效
        //注：当"定义类型"=2, "定义内容"为空时则忽略不播放
        $apn->voicePlayMessage = "定义内容";
        $apn->contentAvailable=1;
        $apn->category="ACTIONABLE";
        $media = new \IGtMultiMedia();
        $media -> set_url("http://docs.getui.com/start/img/pushapp_android.png");
        $media -> set_onlywifi(false);
        $media -> set_type(\MediaType::pic);
        $medias = array();
        $medias[] = $media;
        //$apn->set_multiMedias($medias);
        $template->set_apnInfo($apn);

        return $template;
    }

    public function IGtNotificationTemplateDemo($Message)
    {
        $template = new \IGtNotificationTemplate();
        $template->set_appId($this->AppID);                   //应用appid
        $template->set_appkey($this->AppKey);                 //应用appkey
        $template->set_transmissionType(1);            //透传消息类型
        $template->set_transmissionContent("預約提示：".$Message);//透传内容
        // STEP3：设置推送标题、推送内容
        $template->set_title("預約提示：");      //通知栏标题
        $template->set_text($Message);     //通知栏内容
        $template->set_logo("");                       //通知栏logo
        $template->set_logoURL("");                    //通知栏logo链接
//        STEP4：设置响铃、震动等推送效果
        $template->set_isRing(true);                   //是否响铃
        $template->set_isVibrate(true);                //是否震动
        $template->set_isClearable(true);              //通知栏是否可清除
        return $template;
    }
}
